package com.beiding.workordersystem.common.utils;

import com.beiding.workordersystem.common.standard.E;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import java.io.*;

//文件的上传下载工具类
@Component
public class FileUtils implements InitializingBean {

    private org.slf4j.Logger log = LoggerFactory.getLogger(this.getClass());

    //文件存放位置
    @Value("${uploadFile.path}")
    private String path;

    private File root;

    public InputStream getInputStream(String name) {
        File file = new File(root, name);
        try {
            return new FileInputStream(file);
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    /**
     * 文件上传
     *
     * @param name        上传的文件名称
     * @param inputStream 输入流
     * @return 0成功, 1已经存在, 2目标文件创建失败, 3流复制过程中出现无法预料的异常
     */
    public int write(String name, InputStream inputStream) {

        File file = new File(root, name);
        if (file.exists()) {
            return 1;
        }

        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return 2;
            }
        }

        try {
            if (!file.createNewFile()) {
                return 2;
            }
        } catch (Exception e) {
            return 2;
        }

        FileOutputStream outputStream =null;
        try {
            outputStream = new FileOutputStream(file);
            //进行流复制
            StreamUtils.copy(inputStream, outputStream);
            outputStream.close();
            return 0;
        } catch (Exception e) {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception ignore) {
                }
            }
            file.delete();
            return 3;
        }

    }

    /**
     * 删除文件
     *
     * @param name 被删除的文件名称
     * @return 0成功, 1文件不存在, 2文件删除失败
     */
    public int delete(String name) {

        File file = new File(root, name);

        if (!file.exists()) {
            return 1;
        }

        if (!file.delete()) {
            return 2;
        }

        return 0;
    }


    /**
     * 文件下载
     *
     * @param name         下载的文件名
     * @param outputStream 输出流
     * @return 0成功, 1文件不存在, 2流复制过程中出现异常
     */
    public int read(String name, OutputStream outputStream) {

        File file = new File(root, name);

        if (!file.exists()) {
            return 1;
        }

        try {

            FileInputStream inputStream = new FileInputStream(file);
            StreamUtils.copy(inputStream, outputStream);
            inputStream.close();
            return 0;

        } catch (Exception e) {
            return 2;
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {

        //创建文件路径
        String user = System.getProperties().getProperty("user.home");

        File file = new File(user);

        root = new File(file, path);

        if (!root.exists()) {
            if (!root.mkdirs()) {
                throw new RuntimeException("无法创建文件下载目录:" + root.getAbsolutePath());
            }
        }

        log.info("文件上传下载目录:" + root.getAbsolutePath());

    }

}
